package com.ttbj.product.order.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ttbj.api.dto.Page;
import com.ttbj.api.dto.order.OrderCustomDriverDto;
import com.ttbj.api.dto.order.OrderCustomDto;
import com.ttbj.api.dto.order.OrderWorkDto;
import com.ttbj.api.dto.order.WaybillDto;
import com.ttbj.api.exception.BusinessException;
import com.ttbj.api.order.OrderService;
import com.ttbj.constant.PlatFormErrorCode;
import com.ttbj.constant.annotation.SystemServiceLog;
import com.ttbj.constant.order.OrderIsMoveEnum;
import com.ttbj.constant.order.OrderPayEnum;
import com.ttbj.constant.order.OrderStateEnum;
import com.ttbj.core.util.DtoTransitionUtils;
import com.ttbj.order.bean.po.OrderWork;
import com.ttbj.order.bean.po.WayBill;
import com.ttbj.order.dao.OrderMapper;
import com.ttbj.order.dao.OrderWorkMapper;
import com.ttbj.order.dao.WaybillMapper;

@Service("orderService")
public class OrderServiceImpl implements OrderService{
	
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private OrderWorkMapper orderWorkMapper;
	@Autowired
	private WaybillMapper waybillMapper;
	

	@Override
	public List<OrderCustomDto> findAdvanceSalOrderInfoByPage(OrderCustomDto orderCustomDto, Page page)
			throws BusinessException {
		if (null == orderCustomDto) {
			throw new BusinessException("数据有错误！", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		//设置成预约状态
		orderCustomDto.setOrderState(OrderStateEnum.appointment.getCode());
		//设置成搬运单
		orderCustomDto.setOrderIsMove(OrderIsMoveEnum.carry_order.getCode());
		List<OrderCustomDto> orderCustomDtos = this.orderMapper.queryAdvanceSalOrderInfoByPage(orderCustomDto, page);
		return orderCustomDtos;
	}

	@Override
	public Integer findAdvanceSalOrderInfoByPageCount(OrderCustomDto orderCustomDto) throws BusinessException {
		if (null == orderCustomDto) {
			throw new BusinessException("数据有错误！", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		//查询预约状态的订单
		orderCustomDto.setOrderState(OrderStateEnum.appointment.getCode());
		//设置成搬运单
		orderCustomDto.setOrderIsMove(OrderIsMoveEnum.carry_order.getCode());
		Integer count = this.orderMapper.queryAdvanceSalOrderInfoByPageCount(orderCustomDto);
		return count;
	}
	
	@Override
	public List<OrderCustomDriverDto> findqueryAdvanceDriOrderInfoByPage(Page page)
			throws BusinessException {
		Integer orderAutoComple = 0;	//0:转为手动  1：手动
		List<OrderCustomDriverDto> orderCustomDriverDtos =  this.orderMapper.queryAdvanceDriOrderInfoByPage(orderAutoComple, page);
		return orderCustomDriverDtos;
	}

	@Override
	public Integer findAdvanceDriOrderInfoByPageCount() throws BusinessException {
		Integer orderAutoComple = 0;	//0:转为手动  1：手动
		int count = this.orderMapper.queryAdvanceDriOrderInfoByPageCount(orderAutoComple);
		return count;
	}
	
	@Override
	public List<OrderCustomDto> findAdvanceOrderInfoByPage(OrderCustomDto orderCustomDto, Page page)
			throws BusinessException {
		if (null == orderCustomDto) {
			throw new BusinessException("数据有错误！", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		List<OrderCustomDto> orderCustomDtos = this.orderMapper.queryAdvanceOrderInfoByPage(orderCustomDto, page);
		return orderCustomDtos;
	}

	@Override
	public Integer findAdvanceOrderInfoByPageCount(OrderCustomDto orderCustomDto) throws BusinessException {
		if (null == orderCustomDto) {
			throw new BusinessException("数据有错误！", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		Integer count = this.orderMapper.queryAdvanceOrderInfoByPageCount(orderCustomDto);
		return count;
	}
	
	@Override
	public List<OrderCustomDto> findCompleteOrderInfoByPage(OrderCustomDto orderCustomDto, Page page)
			throws BusinessException {
		if (null == orderCustomDto) {
			throw new BusinessException("数据有错误！", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		//查询支付成功的支付以及账单
		orderCustomDto.setOrderPayState(OrderPayEnum.ALREADY_PAID.getCode());
		List<OrderCustomDto> orderCustomDtos = this.orderMapper.queryCompleteOrderInfoByPage(orderCustomDto, page);
		return orderCustomDtos;
	}

	@Override
	public Integer findCompleteOrderInfoByPageCount(OrderCustomDto orderCustomDto) throws BusinessException {
		if (null == orderCustomDto) {
			throw new BusinessException("数据有错误！", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		//查询支付成功的支付以及账单
		orderCustomDto.setOrderPayState(OrderPayEnum.ALREADY_PAID.getCode());
		Integer count = this.orderMapper.queryCompleteOrderInfoByPageCount(orderCustomDto);
		return count;
	}

	@Override
	@Transactional
	@SystemServiceLog(description="订单分配业务员")
	public void addToOrderWorkInfo(OrderWorkDto orderWorkDto) throws BusinessException {
		if (null == orderWorkDto) {
			throw new BusinessException("数据有错误，分配失败", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		OrderWork orderWork = (OrderWork) DtoTransitionUtils.trans(OrderWork.class, orderWorkDto);
		orderWork.setWorkInsTime(new Date());	//设置添加时间
		//把预订单分配给业务员操作
		int result = this.orderWorkMapper.insertOrderWorkInfo(orderWork);
		if (result != 0) {
			//更改订单状态，由0（预约状态）改为1（业务员接单状态），设置更新时间
			int uResult = this.orderMapper.updateOrderStateById(OrderStateEnum.salesman_order_taking.getCode(), 
					new Date(), orderWorkDto.getWorkOrder());
			if (uResult == 0) {
				throw new BusinessException("分配失败，请重新分配", PlatFormErrorCode.DATA_INSERT_ERROR.code());
			}
		}else {
			throw new BusinessException("分配失败，请重新分配", PlatFormErrorCode.DATA_INSERT_ERROR.code());
		}
	}

	@Override
	@Transactional
	@SystemServiceLog(description="订单分配司机")
	public void addToWaybillInfo(List<WaybillDto> waybillDtos) throws BusinessException {
		if (waybillDtos == null || waybillDtos.size() == 0) {
			throw new BusinessException("数据有错误，分配失败", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		List<WayBill> wayBills = new ArrayList<>();
		for (int i = 0; i < waybillDtos.size(); i++) {
			WayBill wayBill = (WayBill) DtoTransitionUtils.trans(WayBill.class, waybillDtos.get(i));
			wayBill.setWaybillInsTime(new Date());	//设置添加时间
			wayBills.add(wayBill);
		}
		//把订单分配给司机的操作
		int result = this.waybillMapper.insertWaybillInfo(wayBills);
		if (result != 0) {
			//更改订单已派司机的数量
			int uResult = this.orderMapper.updateOrderDriverAlreadyById(waybillDtos.size(), new Date(), 
					waybillDtos.get(0).getWaybillOrder());
			if (uResult == 0) {
				throw new BusinessException("分配失败，请重新分配", PlatFormErrorCode.DATA_INSERT_ERROR.code());
			}
		}else {
			throw new BusinessException("分配失败，请重新分配", PlatFormErrorCode.DATA_INSERT_ERROR.code());
		}
	}

	@Override
	public Long findNewestOrderInfo() {
		Long orderId = this.orderMapper
				.queryNewestOrderInfo(OrderStateEnum.appointment.getCode(), OrderIsMoveEnum.carry_order.getCode());
		return orderId;
	}
}
